import {TBizcEneryEquipApi} from '@/api/equip/tBizcEneryEquipApi.js'

export const FormEvent = {
  OptionData_yxzt: [],
  OptionData_sblxmc: [],
  OptionData_djhszt: [],
  form: {
    ccbm: "",
    ccrq: "",
    cjmc: "",
    cjr: "",
    cjtime: "",
    ct: "",
    gldwbm: "",
    gldwmc: "",
    jlfsbm: "",
    jlfsmc: "",
    ewm: "",
    pt: "",
    sbgg: "",
    sblxbm: "",
    sblxmc: "",
    sbmc: "",
    sbzp: "",
    sbztbm: "",
    sbztmc: "",
    sskfmc: "",
    sskfbm: "",

    sfjc: "",
    wzlbbm: "",
    wzlbmc: "",
    xh: "",
    yxzt: "",
    tyrq: "",
    yxztmc: "",
    sbbm: '',
    deptName: '',      //设备编码
    sbzb: "",
    sftq: "",
    sbdz: ""
  },
  rules: {
    sbmc: [
      {required: true, message: '设备名称 不能为空', trigger: 'blur'},
    ],
    gldwmc: [
      {required: true, message: '管理单位 不能为空', trigger: 'blur'},
    ],
    sskfmc: [
      {required: true, message: '所属库房 不能为空', trigger: 'blur'},
    ],
    sfjc: [
      {required: true, message: '是否已检测 不能为空', trigger: 'blur'},
    ],
    wzlbmc: [
      {required: true, message: '物资类别 不能为空', trigger: 'blur'},
    ],
    sblxmc: [
      {required: true, message: '设备类型 不能为空', trigger: 'blur'},
    ],
    sbgg: [
      {required: true, message: '规格 不能为空', trigger: 'blur'},
    ],
    jlfsmc: [
      {required: true, message: '计量方式 不能为空', trigger: 'blur'},
    ],
    xh: [
      {required: true, message: '型号 不能为空', trigger: 'blur'},
    ],
    ccbm: [
      {required: true, message: '出厂编码 不能为空', trigger: 'blur'},
    ],
    ccrq: [
      {required: true, message: '出厂日期 不能为空', trigger: 'blur'},
    ],
    tyrq: [
      {required: true, message: '投运日期 不能为空', trigger: 'blur'},
    ],
    yxzt: [
      {required: true, message: '运行状态 不能为空', trigger: 'blur'},
    ],
    cjmc: [
      {required: true, message: '厂家 不能为空', trigger: 'blur'},
    ],

  },
  type: '', ////insert-update//
  initFormData(formData) { 		//初始化表单信�?//
    FormEvent.type = formData.type;
    if (FormEvent.type === 'insert') {
      for (let key in FormEvent.form) {
        FormEvent.form[key] = undefined;
      }
      FormEvent.form.wzlbbm = '1000'
      FormEvent.form.wzlbmc = '三省物资'

    } else {
      FormEvent.form = formData
      // for (let key in FormEvent.form) {
      //   FormEvent.form[key] = formData.data[key] || formData[key];
      // }
      console.log(FormEvent.form)
    }
  },
  saveFormData() {
    if (FormEvent.type === 'insert') {
      //执行新增操作//
      const insertData = [FormEvent.form];
      return TBizcEneryEquipApi.insert(insertData);
    } else if (FormEvent.type === 'update') {
      //执行更新操作//
      //判断是否是初始设备
      if (FormEvent.form.sbztbm === '0') {
        FormEvent.form.ewm = '0'
      } else {
        delete FormEvent.form.ewm
      }

      const updateData = [FormEvent.form];
      return TBizcEneryEquipApi.update(updateData);
    }
  },
  getOptionData_yxzt: (xGrid) => {//获取运行状态：字典表
    TBizcEneryEquipApi.queryDictData('EN_EQUIP_YXZT').then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '', value: ''}
        ]
        res.data.forEach((item) => {
          optionList.push({label: item.value, value: item.code})
        })
        // const item = xGrid.getFormItems(1)
        item.itemRender.options = optionList
        FormEvent.OptionData_yxzt = optionList;
      }
    }).catch(err => {
      console.log(err)
    })
  },
  getOptionData_sblxmc: (xGrid) => {//获取设备类型名称：字典表
    TBizcEneryEquipApi.queryDictData('EN_EQUIP_SBLX').then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '', value: ''}
        ]
        res.data.forEach((item) => {
          optionList.push({label: item.value, value: item.code})
        })
        const item = xGrid.getFormItems(4)
        item.itemRender.options = optionList
        OptionData.OptionData_sblxmc = optionList;
      }
    }).catch(err => {
      console.log(err)
    })
  },
  getOptionData_djhszt: (xGrid) => {//获取单据回收状态：字典表
    TBizcEneryEquipApi.queryDictData('EN_EQUIP_DJHSZT').then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '', value: ''}
        ]
        res.data.forEach((item) => {
          optionList.push({label: item.value, value: item.code})
        })
        const item = xGrid.getFormItems(17)
        item.itemRender.options = optionList
        OptionData.OptionData_djhszt = optionList;
      }
    }).catch(err => {
      console.log(err)
    })
  },
}


